/**
 * @author  杨铁心
 * @version 创建时间：2014-9-23 上午9:53:34
 */

package com.mason.meitaopai.main;

import com.mason.meitaopai.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends Activity
{
    private WebView body_wv;
    private ProgressBar body_pb;
    private long mExitTime;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        body_wv = (WebView) findViewById(R.id.body_wv);
        body_pb = (ProgressBar) findViewById(R.id.body_pb);
        body_wv.getSettings().setJavaScriptEnabled(true);
        body_wv.setWebViewClient(new MyWebViewClient());
        body_wv.setWebChromeClient(new WebChromeClient()
        {
            public void onProgressChanged(WebView view , int progress)
            {
                body_pb.setProgress(progress);
                if (progress == 100)
                {
                    body_pb.setVisibility(View.GONE);
                }
                super.onProgressChanged(view, progress);
            }
        });
        body_wv.loadUrl("http://m.meitaopai.com");
    }

    private class MyWebViewClient extends WebViewClient
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view , String url)
        {
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onReceivedError(WebView view , int errorCode , String description , String failingUrl)
        {
            super.onReceivedError(view, errorCode, description, failingUrl);
            body_wv.loadUrl("file:///android_asset/loaderror.html");
        }
    }

    public boolean onKeyDown(int keyCode , KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            if ((System.currentTimeMillis() - mExitTime) > 2000)
            {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            }
            else
            {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
